﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SharedGenomics.Core;

namespace SharedGenomics.Workbench.Views.WindowsForms
{
    public partial class ValidationErrorListView : View, IValidationErrorListView
    {
        public ValidationErrorListView()
        {
            InitializeComponent();
        }

        public event EventHandler ShowErrorsClicked;

        public event EventHandler ShowWarningsClicked;

        public bool IsShowErrorsSelected
        {
            get { return this.tsbShowErrors.Checked; }
            set { this.tsbShowErrors.Checked = value; }
        }

        public bool IsShowWarningsSelected
        {
            get { return this.tsbShowWarnings.Checked; }
            set { this.tsbShowWarnings.Checked = value; }
        }

        public IEnumerable<ValidationMessage> Messages
        {
            set
            {
                this.dgvValidationMessages.DataSource = (value ?? Enumerable.Empty<ValidationMessage>()).ToList();
            }
        }

        private void tsbShowErrors_CheckedChanged(object sender, EventArgs e)
        {
            this.ShowErrorsClicked.Raise(this);
        }

        private void tsbShowWarnings_CheckedChanged(object sender, EventArgs e)
        {
            this.ShowWarningsClicked.Raise(this);
        }

        private void dgvValidationMessages_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex >= 0)
            {
                DataGridViewImageCell cell = (DataGridViewImageCell)this.dgvValidationMessages[e.ColumnIndex, e.RowIndex];
                ValidationMessage message = (ValidationMessage)this.dgvValidationMessages.Rows[e.RowIndex].DataBoundItem;

                int imageDim = (Math.Min(cell.Size.Width, cell.Size.Height) - 6);
                Icon icon = message.Severity == ValidationMessageSeverity.Error
                    ? SystemIcons.Error
                    : SystemIcons.Warning;
                
                cell.Value = icon;
            }
        }
    }
}
